﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using cbmobile.BLC;
using cbmobile.DAC;
using PQT.API;
using PQT.API.File;
using PQT.Common;
using PQT.Controls;
using Telerik.WebControls;
using PQT.API.DataDefine.Sys;

namespace cbmobile.MasterUser.UC_Page
{
    public partial class LKInfo : System.Web.UI.UserControl
    {
        BLC_LinhKien blc = new BLC_LinhKien();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["MA_LK"] != null)
                {
                    string strma_sp = Request.QueryString["MA_LK"].ToLower();
                    int ma_sp = int.Parse(strma_sp);
                    LoadProd(ma_sp);
                }
            }
        }

        private void LoadProd(int ma_LK)
        {
            //Sơ lượt---------------------------------------
            LinhKien lk = blc.Get_Components_By_ID(ma_LK);
            imgSP.ImageUrl = "~/ImagesLK/" + lk.HinhAnh.ToString();
            lbl_Name.Text = lk.TenLinhKien.ToString();
            lblTenLinhKien.Text = lk.TenLinhKien.ToString();
            lblMaSo.Text = lk.Ma_LinhKien.ToString();
            if (lk.TinhTrang == 1)
                lblTinhTrang.Text = "Có hàng";
            else
                lblTinhTrang.Text = "Đã hết hàng";
            lblChatLieu.Text = lk.ChatLieu.ToString();
            lblMauSac.Text = lk.MauSac.ToString();
            lblNguyenHop.Text = lk.NguyenHop.ToString();
            lblBaoHanh.Text = lk.BaoHanh.ToString();
            lblLuotXem.Text = lk.SoLuotXem.ToString();
            lblMoTa.Text = lk.MoTa;
            lblGiaTT.Text = lk.Gia.ToString() + " VND";

            lblThongSo.Text = lk.BaiViet.ToString();

        }

        protected void cmdDatHang_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (lblTinhTrang.Text == "1" || lblTinhTrang.Text == "Có hàng")
                {
                    string ma_sp = Request.QueryString["Ma_PK"];
                    string hinh_sp = imgSP.ImageUrl;
                    string ten_sp = lblTenLinhKien.Text;
                    string mau_sp = lblMauSac.Text;

                    decimal gia_sp = Convert.ToDecimal(lblGiaTT.Text.Remove(lblGiaTT.Text.IndexOf("V")));
                    int soluong = 1;
                    decimal tt = soluong * gia_sp;
                    ThemVaoGioHang(ma_sp, soluong, gia_sp, mau_sp, hinh_sp, ten_sp, tt);
                    if (Session["slsp"] == null)
                    {
                        Session["slsp"] = 1;
                    }
                    else
                    {
                        int slsp = Convert.ToInt32(Session["slsp"].ToString());
                        slsp += 1;
                        Session["slsp"] = slsp;
                    }
                    Response.Redirect("ChiTietGioHang.aspx");
                }

            }
        }

        private void ThemVaoGioHang(string masp, int soluong, decimal gia, string mau, string hinh, string tensp, decimal thanhtien)
        {
            DataTable giohang;
            if (Session["addGioHang"] == null)
            {
                giohang = new DataTable();
                giohang.Columns.Add("MaSP");
                giohang.Columns.Add("Hinh");
                giohang.Columns.Add("TenSP");
                giohang.Columns.Add("MauSac");
                giohang.Columns.Add("Gia");
                giohang.Columns.Add("SoLuong");
                giohang.Columns.Add("ThanhTien");
                giohang.Columns.Add("LoaiSP");
            }
            else
            {
                giohang = (DataTable)Session["addGioHang"];
            }
            int indexofitem = IsExistItemInShoppingCart(masp, giohang);
            if (indexofitem != -1)
            {
                giohang.Rows[indexofitem]["SoLuong"] = Convert.ToInt32(giohang.Rows[indexofitem]["SoLuong"]) + 1;
                giohang.Rows[indexofitem]["ThanhTien"] = gia * Convert.ToDecimal(giohang.Rows[indexofitem]["SoLuong"]);
            }
            else
            {
                DataRow datarow = giohang.NewRow();
                datarow["MaSP"] = masp;
                datarow["Hinh"] = hinh;
                datarow["TenSP"] = tensp;
                datarow["MauSac"] = mau;
                datarow["Gia"] = gia;
                datarow["SoLuong"] = 1;
                datarow["ThanhTien"] = gia;
                datarow["LoaiSP"] = "LK";
                giohang.Rows.Add(datarow);
            }
            Session["addGioHang"] = giohang;
        }

        private int IsExistItemInShoppingCart(string itemID, DataTable dataTable)
        {
            int indexOfRow = -1;
            for (int dataRow = 0; dataRow < dataTable.Rows.Count; dataRow++)
            {
                if (dataTable.Rows[dataRow]["MaSP"].ToString() == itemID)
                {
                    indexOfRow = dataRow;
                    break;
                }
            }
            return indexOfRow;
        }
    }
}